<template>
  <div class="view-fill">
    <div v-if="!modifyInfo.visible" class="app-container-flex">
      <NewFullScreen
        v-model="showDialog"
        @returnBack="returnBack"
        title="申购模板明细"
      >
        <template v-slot:full-button>
          <el-button
            @click="update()"
            v-if="$permission('purchSubscribeTemplate:update')"
            >修改</el-button
          >
          <el-button
            v-if="$permission('purchSubscribeTemplate:exportExcel')"
            @click="exportExcel"
            >导出excel</el-button
          >
        </template>
        <div class="model-panel">
          <div class="model-panel-title">
            <p>{{ $t('basic_information') }}</p>
          </div>
          <div class="model-panel-body">
            <!-- form -->
            <BasicInformation
              :info="basicInfo.info"
              :label-width="basicInfo.labelWidth"
            />
          </div>
        </div>

        <div class="model-panel">
          <div class="model-panel-title">
            <Tooltip :text="$t('detailInfo')"></Tooltip>
          </div>
          <div class="model-panel-body">
            <div class="flex_full_item">
              <!-- 表格 -->
              <PageTable
                :refresh="tableInfo.refresh"
                :init-curpage="tableInfo.initCurpage"
                :check-box="false"
                :tabIndex="true"
                v-model:data="tableInfo.data"
                :pager="false"
                :summary="true"
                v-model:query="filterInfo.query"
                :field-list="tableInfo.fieldList"
                :summaryFieldList="tableInfo.summaryFieldList"
                :handle="tableInfo.handle"
                :isDynamic="true"
                @handleEvent="handleEvent"
              >
              </PageTable>
            </div>
          </div>
        </div>
      </NewFullScreen>
    </div>
    <purch-template-modify
      v-if="modifyInfo.visible"
      v-model:visible="modifyInfo.visible"
      :title="modifyInfo.title"
      :purchTemplateId="modifyInfo.purchTemplateId"
    />
  </div>
</template>

<script setup name="purchTemplateDetail">
import {
  getPurchTemplateById,
  getPurchTemplateDetailAll,
  getPurchTemplateDetailCustomizeList,
  getPurchTemplateExcel,
} from '@/api/purch'
import { getCustomizeSetting } from '@/api/customize'
import PurchTemplateModify from './modify'
const { proxy } = getCurrentInstance()
const emits = defineEmits()
const props = defineProps({
  visible: {
    type: Boolean,
    default: false,
  },
  id: {
    type: String,
    default: () => {
      return 0
    },
  },
})
const basicInfo = ref({
  info: {
    data: {},
    fieldList: [
      { label: '模板名称', value: 'name' },
      { label: '部门', value: 'deptName' },
      { label: '备注', value: 'remark', type: 'popover' },
    ],
  },
  labelWidth: '130px',
})
// 过滤相关配置
const filterInfo = ref({
  query: {
    customizeUserVO: {},
    queryMap: {
      purchTemplateId: {
        queryType: 'EQUALS',
        queryCategory: 'AND',
        value: props.id,
      },
    },
  },
})
// 表格相关
const tableInfo = ref({
  refresh: 1,
  initCurpage: 1,
  api: getPurchTemplateDetailAll,
  data: [],
  selections: [],
  summaryFieldList: [
    {
      value: 'qty',
      formatter: proxy.$fn.qtyFormatter,
    },
    {
      value: 'sumAmount',
      formatter: proxy.$fn.TripartiteMethod,
    },
  ],
  fieldList: [],
})
const modifyInfo = ref({
  visible: false,
  title: '',
  purchTemplateId: '',
})
watch(
  () => modifyInfo.value.visible,
  (val) => {
    if (!val) {
      getMain()
      getList()
    }
  }
)

const showDialog = computed({
  get() {
    return props.visible
  },
  set(val) {
    emits('update:visible', val) // 更新父组件visible
  },
})

function returnBack() {
  emits('returnBack', false)
}
// 获取列表
function getList() {
  getPurchTemplateDetailCustomizeList(filterInfo.value.query).then((resp) => {
    console.log(resp)
    if (resp && resp.success) {
      tableInfo.value.data = resp.data
    }
  })
}
// 搜索
function search() {
  // 重置分页
  tableInfo.value.initCurpage = Math.random()
  tableInfo.value.refresh = Math.random()
}
//导出excel
function exportExcel() {
  getPurchTemplateExcel(filterInfo.value.query).then((resp) => {
    if (resp) {
      proxy.$fn.parseExcel(resp)
    }
  })
}
function getMain() {
  getPurchTemplateById(props.id).then((resp) => {
    basicInfo.value.info.data = resp.data
    getList()
  })
}
// 触发事件
function handleEvent(event, data) {
  switch (event) {
    case 'tableCheck':
      tableInfo.value.selections = [...data]
      break
  }
}
//修改
function update() {
  modifyInfo.value.title = '修改采购模板'
  modifyInfo.value.purchTemplateId = props.id
  modifyInfo.value.visible = true
}
function getCustomize() {
  let param = {
    tableKey:
      'purchSubTemplateAddAndModifyDetail-com.slip.spd.entity.vo.purch.PurchTemplateDetailVO',
  }
  console.log('表单列' + 111)
  getCustomizeSetting(param).then((res) => {
    if (res && res.success) {
      let data = res.data
      filterInfo.value.query.customizeUserVO = res.data
      tableInfo.value.fieldList = proxy.$fn.setFieldList(
        tableInfo.value.fieldList,
        data.detail
      )
      console.log('表单列' + tableInfo.value.fieldList)
      getMain()
    }
  })
}

let obj = {
  price: {
    label: '单价',
    value: 'price',
    type: 'column',
    formatter: proxy.$fn.TripartiteMethod,
    permission: !proxy.$permission('purchSubscribeTemplate:price'),
    hidden: true,
  },
  sumAmount: {
    label: '总金额',
    value: 'sumAmount',
    type: 'column',
    formatter: proxy.$fn.TripartiteMethod,
    permission: !proxy.$permission('purchSubscribeTemplate:price'),
    hidden: true,
  },
  defaultSupplierPrice: {
    label: '默认供应商价格',
    value: 'defaultSupplierPrice',
    type: 'column',
    formatter: proxy.$fn.TripartiteMethod,
    permission: !proxy.$permission('purchSubscribeTemplate:price'),
    hidden: true,
  },
}
tableInfo.value.fieldList = Object.values({
  ...proxy.$fn.deepClone(proxy.$fieldfList.FieldList),
  ...obj,
})
getCustomize()
</script>

<style scoped></style>
